<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
class user {

	var $userid;
	var $password;
	var $mail;
	var $groupid;
	var $teamid;	
	var $timezone;
	var $lang;
	var $status;
	var $team;
	var $currentpwd;
	
	var $passexpire;
	var $passcode;
	
	var $options;

	public function __construct()
   {
   }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('user');	
		//return $class->newInstance();
		return new user;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".USERS." WHERE userid = ".util::str($this->userid);
		return $sql;
	}

	public function listAll() {
		$sql = "SELECT * FROM ".USERS." ORDER BY userid";
		return $sql;
	}

	public function listByStatus() {
		$sql = "SELECT * FROM ".USERS." WHERE status = ".util::str($this->status)." ORDER BY userid";
		return $sql;
	}

	public function listByFilters() {
		$sql = "SELECT * FROM ".USERS;
		$filter = "";
		if ($this->status != null) {
			$filter .= " status = ".util::str($this->status);
		}
		if ($this->teamid != null) {
			if ($filter != "") $filter .= " AND ";			
			$filter .= " teamid = ".util::str($this->teamid);
		}
		if ($this->groupid != null) {
			if (count($this->groupid)>1) {
				if ($filter != "") $filter .= " AND ";
				$filter .= " (groupid = ".util::str($this->groupid[0]);
				for ($i=1;$i<count($this->groupid);$i++) {
					$filter .= " OR groupid = ".util::str($this->groupid[$i]);
				}
				$filter.=")";
			} else {
				if ($filter != "") $filter .= " AND ";
				$filter .= " groupid = ".util::str($this->groupid);
			}
		}

		if ($filter != "") $sql .= " WHERE " .$filter;
		$sql .=" ORDER BY userid";
		return $sql;
	}
	
	public function listByGroup() {
		$sql = "SELECT * FROM ".USERS." WHERE groupid = ".util::str($this->groupid)." ORDER BY userid";
		return $sql;
	}	
	
	public function insert() {
		$sql = "INSERT INTO ".USERS." (userid, mail, password, groupid, timezone, lang, teamid, status,options,passcode,passexpire) ";
		$sql .= "VALUES (".util::str($this->userid).",".util::str($this->mail).",".util::str(util::encrypt($this->password)).",";
		$sql .= util::str($this->groupid).",".util::str($this->timezone).",".util::str($this->lang).",".util::str($this->teamid);
		$sql .= ",".util::str($this->status).",".util::str(DEFAULT_USER_OPTIONS).",".util::str($this->passcode).",".util::str($this->passexpire).")";
		return $sql;
	}

	public function update() {
		$opfusion = "";
		foreach ($this->options as $value) {
			$opfusion .= $value;
		}
		$sql = "UPDATE ".USERS;
		$sql .= " set mail = ".util::str($this->mail);
		$sql .= ", groupid = ".util::str($this->groupid).", timezone = ".util::str($this->timezone).", lang = ".util::str($this->lang);
		$sql .= ", status = ".util::str($this->status).", teamid = ".util::str($this->teamid);
		$sql .=", passexpire = ".util::str($this->passexpire);
		$sql .=", passcode = ".util::str($this->passcode);
		$sql .=", options = ".util::str($opfusion);
		if ($this->password!='') {
			$sql .=", password = ".util::str(util::encrypt($this->password));
		}
		$sql .= " WHERE userid = ".util::str($this->userid);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".USERS." where userid = ".util::str($this->userid);
		return $sql;
	}

	public function validate($pwd) {
		return $this->currentpwd == util::encrypt($pwd);
	}

	public function validateNoEncrypt($pwd) {
		return $this->currentpwd == $pwd;
	}
		
	public function fill($array) {
		$this->userid=$array['userid'];
		$this->mail=$array['mail'];
		$this->currentpwd=$array['password'];
		$this->groupid=$array['groupid'];
		$this->timezone=$array['timezone'];		
		$this->lang=$array['lang'];
		$this->status=$array['status'];
		$this->teamid=$array['teamid'];
		$this->passcode=$array['passcode'];
		$this->passexpire=$array['passexpire'];
		$this->options=str_split($array['options']);
		//$this->options=$array['options'];
	}
	
	private function setOption($val,$var) {

		if ($val) {
			if (!$this->getOption($var)) {
				array_push($this->options,$var);	
			}
		} else {
			$temp = array();
			foreach ($this->options as $value) {
				if ($value != $var) {
					array_push($temp,$value);
				}
			}
			$this->options = $temp;
		}

	}
	
	private function getOption($var) {
		if (array_search($var,$this->options)===false) return false;
		return true;
	}
	
	public function allowUserEmail() {
		return $this->getOption("A");
	}

	public function setAllowUserEmail($val) {
		$this->setOption($val,"A");
	}

	public function notifyOnChange() {
		return $this->getOption("B");
	}

	public function setNotifyOnChange($val) {
		$this->setOption($val,"B");
	}

	public function notifyTask() {
		return $this->getOption("C");
	}

	public function setNotifyTask($val) {
		$this->setOption($val,"C");
	}
/*
	public function commitRights() {
		return $this->getOption("D");
	}

	public function setCommitRights($val) {
		$this->setOption($val,"D");
	}
*/
}

?>
